package MyFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter("/*")
public class MyFilter extends HttpFilter {
	@Override
	public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
		HttpServletResponse response=(HttpServletResponse) res;
		//对这个端口进行访问
		response.setHeader("Access-Control-Allow-Origin", "http://localhost:8888");
		response.setHeader("Access-Control-Allow-Credentials", "true");
		response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PUT");
		response.setHeader("Access-Control-Max-Age", "3628800");
		response.setHeader("Access-Control-Allow-Headers", "x-requested-with,Authorization,content-type");
		req.setCharacterEncoding("UTF-8");
		res.setContentType("application/json;charset=UTF-8");
		chain.doFilter(req,res);
	}
}
